<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2022-08-25 21:33:51
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Models;

use App\Repositories\FinanceVoucherRepository;
use App\Traits\CompanyTrait;
use App\Traits\VoucherTrait;

class FinanceAcceptanceRec extends BaseModel
    {
    use CompanyTrait;
    use VoucherTrait;
    public $timestamps = true;
    public $table = "sys_finance_rec_acceptance";
    protected $guarded = [];
    // 
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'AcceptanceStatus');
            });
        }

    public function billtrade()
        {
        return $this->belongsTo(CompanyBilltrade::class);
        }
    public function buyaccount()
        {
        return $this->belongsTo(Subject::class);
        }
    public function account()
        {
        return $this->belongsTo(Subject::class);
        }

    /**
     * 过账
     *
     * @param  int|string $gdata
     * @return boolean
     */
    public function finance($orders = null)
        {

        $v = $orders ?? $this;
        if ($v->status_id == 1) {
            return $v;
            }
        if ($this->status_id == 2) {
            return $v;
            }
        if ($v->status_id != 0) {
            fail("单据{$v->status->name}！，无法财审！");
            }

        $summary = $v['type'] . "承兑" . $v['code'] . ', ' . $v['remark'];

        $details         = [];
        $auxiliary_data  = [];
        $creadit_subject = "";
        if ($v['type'] == '收') {
            // 贷：应收账款
            $auxiliary_data['company_id']     = $v->customer_id;
            $creadit_subject                  = 1122;
            $auxiliary_data['invoice_amount'] = 0;
            }
        else if ($v['type'] == '购') {
            // 贷：银行存款 
            $creadit_subject = $v->buyaccount_id;
            }
        else {
            // 贷：承兑借款
            $creadit_subject = 200105;
            }

        $details[] = FinanceVoucherRepository::mergeVoucher($creadit_subject, $summary, 0, $v['amount'], $auxiliary_data, 0, 1);
        // 借：应收票据.承兑账号
        $details[]            = FinanceVoucherRepository::mergeVoucher(1121, $summary, $v['amount'], 0, [], 0, 1);
        $voucher              = FinanceVoucherRepository::CreateVoucher($v->recedate ?? $v->created_at, $v->handletrade_id, $details, $v, $v->voucher?->id);
        $this->handvoucher_id = $voucher->id;
        auditSave($v);
        return FinanceVoucherRepository::auditVoucher($voucher);
        // # 开始保存 -- 贴息金额   
        }
    public function debtFinace()
        {
        if ($this->status_id != 1) {
            fail("承兑【{$this->status->name}】,入账失败！");
            }
        if ($this->status_id == 2) {
            return $this;
            }
        $summry               = "承兑入账{$this->code}{$this->debtorremark}";
        $data                 = $this->toArray();
        $data['billdate']     = $this->recedate;
        $data['company_id']   = 0;
        $data['tax']          = 1;
        $details[]            = FinanceVoucherRepository::mergeVoucher(1121, $summry, 0, $this->amount, $data);
        $details[]            = FinanceVoucherRepository::mergeVoucher($this->account_id, $summry, $this->amount, 0, $data);
        $voucher              = FinanceVoucherRepository::CreateVoucher($this->recedate, $this->trade_id, $details, $this, $this->debtVoucher?->id);
        $this->debtvoucher_id = $voucher->id;
        auditSave($this, true, true, 2);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }
    public function nofinance($orders = null, $voucher = null)
        {
        $v              = $orders ?? $this;
        $handvoucher_id = $v->handvoucher_id;
        $debtvoucher_id = $v->debtvoucher_id;
        if ($voucher) {
            if ($voucher->id == $handvoucher_id) {
                if ($debtvoucher_id > 0) {
                    fail("承兑已到期入账！不能作废【持有】时产生的凭证，必须先作废入账凭证");
                    }
                if ($v->leftamount != $v->amount) {
                    fail("承兑已使用！不能作废【持有】时产生的凭证，必须先作废使用凭证");
                    }
                $v->handvoucher_id = 0;
                auditSave($v, false);
                }
            else {
                $v->leftamount     = $v->_leftamount;
                $v->debtvoucher_id = 0;
                auditSave($v, false, true, 1);
                }
            }
        return $v;
        }
    }
